package com.analysis.core;

import org.objectweb.asm.Opcodes;
import java.util.HashSet;
import java.util.Set;

public class MethodInfo {
    private final String name;
    private final String descriptor;
    private final int access;
    private final Set<String> annotations = new HashSet<>();
    private String apiPath;
    private String httpMethod;

    public MethodInfo(String name, String descriptor, int access) {
        this.name = name;
        this.descriptor = descriptor;
        this.access = access;
    }

    public void addAnnotation(String annotation) {
        annotations.add(annotation);
        if (annotation.contains("RequestMapping")) {
            httpMethod = "ANY";
        } else if (annotation.contains("GetMapping")) {
            httpMethod = "GET";
        } else if (annotation.contains("PostMapping")) {
            httpMethod = "POST";
        } else if (annotation.contains("PutMapping")) {
            httpMethod = "PUT";
        } else if (annotation.contains("DeleteMapping")) {
            httpMethod = "DELETE";
        }
    }

    public void setApiPath(String path) {
        this.apiPath = path;
    }

    public boolean isPublic() {
        return (access & Opcodes.ACC_PUBLIC) != 0;
    }

    public boolean isApiMethod() {
        return httpMethod != null;
    }

    public String getName() {
        return name;
    }

    public String getDescriptor() {
        return descriptor;
    }

    public String getApiPath() {
        return apiPath;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof MethodInfo)) {
            return false;
        }
        MethodInfo other = (MethodInfo) obj;
        return name.equals(other.name) && descriptor.equals(other.descriptor);
    }

    @Override
    public int hashCode() {
        return name.hashCode() * 31 + descriptor.hashCode();
    }
}
